<?
$pagename=LA_NOTIFY;
include_once("header_inc.php");
check_valid_user("");
include_once("member_header.php");
$valid_user = $_SESSION["valid_user"];
$tplExtra=new TplLoad();
$ad_id=getParamInt("ad_id",0);
$array = getPaqueteMainTypeFromAdId($ad_id,true);
$mainType  = $array['mainType'];
$packageId = $array['package_id'];

if ($set_payments)
{
	if (getParam("ad_id","")=="")
	{
		$userAds = getUserAds($valid_user,array("ad_is_special <> 1","ad_is_premium <> 1"));
		$tplExtra->assign("userAds",$userAds);
		
	}else{
		if (getParam("ad_id","") && getParamInt("confirm","")==1)
		{
	        if (getParam("yes","")!="")
			{
				//Validamos que haya creditos en su cuenta
				$sqlCheckCredits = "select pay_premium from mvial_user_credits where user_id=$valid_user and mainType=$mainType and package_id=$packageId";
				$res_credits=q($sqlCheckCredits);
				$rowCredits=mysql_fetch_array($res_credits);
				if($rowCredits["pay_premium"]<=0){
					//Verificamos los paquetes EXTRA
					$mainType = getExtraMainType($mainType);
					$sqlCheckCredits = "select pay_premium,package_id from mvial_user_credits where user_id=$valid_user and mainType=$mainType and pay_premium >0 order by pay_premium DESC limit 1";			
					$res_credits=q($sqlCheckCredits);
					$rowCredits=mysql_fetch_array($res_credits);
					$packageId = $rowCredits["package_id"];
					if(empty($rowCredits["pay_premium"]) || $rowCredits["pay_premium"]<=0 ){
						$tplExtra->assign("pay_premium",$rowCredits["pay_premium"]);
					}
				}
				//else para ambos casos, creditos comunes o EXTRA 
				if($rowCredits["pay_premium"]>0){
						//Fin validación
						$sql="update $ads_tbl set ad_is_special = 1 where ad_id=" . getParamInt("ad_id","");
						$sql.=" AND ad_is_special <> 1";
						$sql.=" AND ad_owner = $valid_user";		
						$res_pay=q($sql);
						
						if (mysql_affected_rows()>0)
						{
							$sql="update $user_credits set pay_bold = pay_bold - 1 where pay_bold>0 ";
							$sql.=" AND user_id = $valid_user and mainType=".$mainType." and package_id=".$packageId;
							$res_pay=q($sql);
						}	
						
						$tplExtra->assign("status",LA_MEMBER_MAKE_BOLD_DONE);
								
						$sql="update $ads_tbl set ad_is_premium = 1 where ad_id=" . getParamInt("ad_id","");
						$sql.=" AND ad_owner = $valid_user";
						$sql.=" AND ad_is_premium <> 1 ";		
						$res_pay=q($sql);
						
						if (mysql_affected_rows()>0)
						{
							$sql="update $user_credits set pay_premium = pay_premium - 1 where pay_premium>0";			
							$sql.=" AND user_id = $valid_user and mainType=".$mainType." and package_id=".$packageId;				
							$res_pay=q($sql);
						}			
						
						$tplExtra->assign("status",LA_MEMBER_MAKE_PREM_DONE);	
				} //else fin validacion creditos
			}
		}
		$lSql="select ad_id,ad_cat_id,ad_title,ad_is_special,ad_is_premium from $ads_tbl where ad_owner=$valid_user and ad_id=$ad_id";
		$lRes=q($lSql);
		$i=0;
		$isPremium = 0;
		$isBold = 0;
		if($row=mysql_fetch_array($lRes))
		{
			if ($row["ad_is_special"]==1)
				$isBold = 1;
			if ($row["ad_is_premium"]==1)
				$isPremium = 1;
				
			$tplExtra->assign("isBold",$isBold);
			$tplExtra->assign("isPremium",$isPremium);				
			$tplExtra->assign("title",$row["ad_title"]);
			$tplExtra->assign("ad_cat_id",$row["ad_cat_id"]);
			$tplExtra->assign("ad_id",$ad_id);		
		}
		if($isPremium==0 || $isBold==0){
			$sql="select pay_membership_expire,pay_upload,pay_upload_video,pay_vendor,pay_credits,pay_bold,pay_premium from $user_credits where user_id=" . $valid_user . " and mainType=".$mainType." and package_id=".$packageId." limit 1";
			$r=q($sql);
			$ro=mysql_fetch_array($r);
			if($ro["pay_premium"]<=0){
					//Verificamos los paquetes EXTRA
					$mainType = getExtraMainType($mainType);
					$sql = "select pay_membership_expire,pay_upload,pay_upload_video,pay_vendor,pay_credits,pay_bold,pay_premium,package_id from mvial_user_credits where user_id=$valid_user and mainType=$mainType and pay_premium >0 order by pay_premium DESC limit 1";			
					$r=q($sql);
					$ro=mysql_fetch_array($r);
					$packageId = $ro["package_id"];					
			}
			$lExpireDate=formatDateShort($ro["pay_membership_expire"]);
			
			if (getExpireStatus($valid_user))
				$tplExtra->assign("expire_date",$lExpireDate);
				
			$tplExtra->assign("pay_upload" ,$ro["pay_upload"]);
			$tplExtra->assign("pay_credits",$ro["pay_credits"]);
			$tplExtra->assign("pay_bold"   ,$ro["pay_bold"]);
			$tplExtra->assign("pay_premium",$ro["pay_premium"]);
		}
	}
	//$tplExtra->assign("listArray",$listArray);
}


$tplExtra->display("member_extra.tpl");
include_once("member_footer.php");
include_once("footer_inc.php");
?>